package admin

import (
	"editor/constant"
	"editor/service/mdFile"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"net/http"
)

func SaveContentHandler(c *gin.Context) {

	id := cast.ToInt64(c.Param("id"))
	info, err := mdFile.GetMDFileInfo(c, id)
	if err != nil {
		c.JSON(http.StatusOK, constant.Response{Msg: "get markdown file info failed", Code: 1})
		return
	}
	if info == nil {
		c.JSON(http.StatusOK, constant.Response{Msg: "get markdown file info empty", Code: 2})
		return
	}

	err = mdFile.UpdateMDFileContent(c, id, c.PostForm("txt"))
	if err != nil {
		c.JSON(http.StatusOK, constant.Response{Msg: "update markdown file info failed", Code: 3})
		return
	}

	c.JSON(http.StatusOK, constant.Response{Msg: "success", Code: 0})
}